app.controller('decorateModify', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope) {
    var data = angular.copy($scope.ngDialogData, {});

    $scope.params = {
      landlordId: user.get('landlordId')
    };

    // 支付方式
    var payTypeList = ['一月一付','三月一付','六月一付','一年一付'];

    $scope.column2 = 0;      //分栏
    $scope.maxImgNum = 10;    //最大图片数量
    $scope.imgNew= [
        {
            text:'合同图片集合',
            name: 'contractPics',
            value: []
        },
        {
            text:'路牌照片',
            name: 'roadPics',
            value: [] 
        },
        {
            text:'门牌照片',
            name: 'doorplatePics',
            value: []
        },
        {
            text:'半开门照片',
            name: 'doubleDoorPics',
            value: []
        },
        {
            text:'房产证照片',
            name: 'houseCardPics',
            value: []
        },
        {
            text:'小区门口照片',
            name: 'doorPhotoPics',
            value: []
        }
    ];

    //从后台获取数据并进行展示
    rest.post({
        methodId: '4015',
        data: {
            decorateId: data.decorateId
        },
        success: function(res) {
            if (res.code == 0) {
              var data1 = res.res.data;
              $scope.houseId = data1.houseId;
              $scope.contractStartDate = data1.contractStartDate;
              $scope.contractEndDate = data1.contractEndDate;
              uploadConf.imgNew[0].value = data1.contractPics;
              uploadConf.imgNew[1].value = data1.roadPics;
              uploadConf.imgNew[2].value = data1.doorplatePics;
              uploadConf.imgNew[3].value = data1.doubleDoorPics;
              uploadConf.imgNew[4].value = data1.houseCardPics;
              uploadConf.imgNew[5].value = data1.doorPhotoPics;
              $scope.expectAmount = data1.expectAmount;
              $scope.contractStartDate = moment(data1.contractStartDate).format('YYYY/MM/DD');
              $scope.contractEndDate = moment(data1.contractEndDate).format('YYYY/MM/DD');
      
              $scope.params.contractStartDate = moment(data1.contractStartDate).format('YYYY/MM/DD');
              $scope.params.contractEndDate = moment(data1.contractEndDate).format('YYYY/MM/DD');
              $scope.params.contractPics = $scope.contractPics || [];
              $scope.params.roadPics = $scope.roadPics || [];
              $scope.params.doorplatePics = $scope.doorplatePics || [];
              $scope.params.doubleDoorPics = $scope.doubleDoorPics || [];
              $scope.params.houseCardPics = $scope.houseCardPics || [];
              $scope.params.doorPhotoPics = $scope.doorPhotoPics || [];
            }
            $scope.$digest();
        },
        error: function() {}
    })

    //获取房源模板列表
    var param_3012 = {};
    param_3012.landlordId = $scope.params.landlordId;
    rest.post({
        methodId: '3012',
        data: param_3012,
        success: function(res) {
            console.log( "3012",res );
            if (res.code == 0) {
                $scope.picModel = { selected: {name: '请选择模板', value: true} };
                $scope.picModelList = res.res.data;
                //console.log($scope.picModelList);
                $scope.$apply();
            } else {

            }
            $scope.disable_save_btn = false;
        },
        error: function() {
            $scope.disable_save_btn = false;
        }
    });

    $scope.chooseModel = function(index,picModelList) {
        $scope.editPicFlag = true;

        $scope.imgNew[0].value = [];
        $scope.imgNew[1].value = [];
        $scope.imgNew[3].value = [];
        $scope.imgNew[4].value = [];
        $scope.imgNew[5].value = [];
        var picModelId = picModelList[index].id;
        // console.log( picModelId );

        var param_3013 = {};
        param_3013.id = picModelId;
        $rootScope.modelId = picModelId;

        rest.post({
            methodId: '3013',
            data: param_3013,
            success: function(res) {
                if (res.code == 0) {
                    var data = res.res.data.content;
                    data = JSON.parse( data );
                    for (var i in data) {
                        if (data[i].PicType == 11) {
                            $scope.imgNew[0].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 12) {
                            $scope.imgNew[1].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 13) {
                            $scope.imgNew[3].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 14) {
                            $scope.imgNew[4].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 15) {
                            $scope.imgNew[5].value.push(data[i].PicPath);
                        } 
                    }
                }
                $scope.disable_save_btn = false;
                $scope.$apply();
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
        $scope.$apply();
    }

    uploadConf.column2 = $scope.column2;
    uploadConf.maxImgNum =  $scope.maxImgNum;
    uploadConf.imgNew = $scope.imgNew;

    $scope.addModelPics = function () {
        // $scope.closeThisDialog();
        $state.go('app.houseOwner.housePicsTemplet');
    }

    $scope.editModelPics = function(templetId){
        $window.localStorage.setItem('housePicId',JSON.stringify({id:$rootScope.modelId}));
        // $scope.closeThisDialog();
        $state.go('app.houseOwner.editHousePicsTemplet',{id: $rootScope.modelId});
    }

    //点击确定按钮
    $scope.modifySure = function() {

      $scope.params.expectAmount = $scope.expectAmount || 0;
      $scope.params.landlordId = user.get('landlordId');

      $scope.params.houseId = $scope.houseId;
      $scope.params.decorateId = data.decorateId;

      $scope.params.contractPics = [];
      $scope.params.roadPics = [];
      $scope.params.doorplatePics = [];
      $scope.params.doubleDoorPics = [];
      $scope.params.houseCardPics = [];
      $scope.params.doorPhotoPics = [];

      for (var i in uploadConf.imgNew[0].value) {
          $scope.params.contractPics.push(uploadConf.imgNew[0].value[i]);
      }
      for (var i in uploadConf.imgNew[1].value) {
              $scope.params.roadPics.push(uploadConf.imgNew[1].value[i]);
      }
      for (var i in uploadConf.imgNew[2].value) {
              $scope.params.doorplatePics.push(uploadConf.imgNew[2].value[i]);
      }
      for (var i in uploadConf.imgNew[3].value) {
              $scope.params.doubleDoorPics.push(uploadConf.imgNew[3].value[i]);
      }
      for (var i in uploadConf.imgNew[4].value) {
              $scope.params.houseCardPics.push(uploadConf.imgNew[4].value[i]);
      }
      for (var i in uploadConf.imgNew[5].value) {
              $scope.params.doorPhotoPics.push(uploadConf.imgNew[5].value[i]);
      }

      if ($scope.params.contractPics.length<1) {
        tipService.show("body", {
            message: "合同图片必传",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.roadPics.length<1) {
        tipService.show("body", {
            message: "路牌照片必传",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.doorplatePics.length<1) {
        tipService.show("body", {
            message: "门牌照片必传",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.doubleDoorPics.length<1) {
        tipService.show("body", {
            message: "半开门照片必传",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.houseCardPics.length<1) {
        tipService.show("body", {
            message: "房产证照片必传",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.doorPhotoPics.length<1) {
        tipService.show("body", {
            message: "小区门口照片必传",
            notice:"danger"
        }, 3);
        return false;
      }

      rest.post({
          methodId: '4016',
          data: $scope.params,
          success: function(res) {
            // $state.go('app.houseOwner.list');
          },
          error: function() {}
      })
        $scope.confirm({});
    }

});

app.controller('modifyContractTimeCtrl',function($scope){
  $scope.today = function() {
      // $scope.contractStartDate = moment().format('YYYY/MM/DD');
      // $scope.contractEndDate = moment().add(2,'years').subtract(1, 'days').format('YYYY/MM/DD');
    };

    $scope.closeAll = function() {
      $scope.openContractBeginDated =
      $scope.openContractEndDated = false;
    }

    $scope.openContractBeginDate = function($event) {
      $event.preventDefault();$event.stopPropagation();$scope.closeAll();
      $scope.openContractBeginDated = true;
    };
    $scope.openContractEndDate = function($event) {
      $event.preventDefault();$event.stopPropagation();$scope.closeAll();
      $scope.openContractEndDated = true;
    };

    $scope.dateOptions = {
      format: 'yyyy/MM/dd',
      startingDay: 1,
      class: 'datepicker'
    };

    $scope.format = 'yyyy/MM/dd';

    $scope.today();

    $scope.$watch('contractStartDate',function(newValue,oldValue){
      if("[object Date]" === Object.prototype.toString.call(newValue)) {
         $scope.$emit('contractDate.start',moment(newValue).format('YYYY/MM/DD'))
      }
    });
    $scope.$watch('contractEndDate',function(newValue,oldValue){
      if("[object Date]" === Object.prototype.toString.call(newValue)) {
         $scope.$emit('contractDate.end',moment(newValue).format('YYYY/MM/DD'))
      }
    });
});
